/*

=============================================================================

Copyright (c) 2010 Xiotex Studios Ltd.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The end-user documentation included with the redistribution, if any, must
include the following acknowledgment: "This product includes software
developed by Xiotex Studios Ltd (http://xiotexstudios.com)", in the same
place and form as other third-party acknowledgments. Alternately, this
acknowledgment may appear in the software itself, in the same form and
location as other such third-party acknowledgments.

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. 

=============================================================================

File:		CSWFFileStreamImplWin32.cpp
Author:		(Ola)dotun Rominiyi


Update(s):	

=============================================================================

*/

#include "SWFBase.h"
#include "CSWFFileStream.h"

#include <stdio.h>

struct _fileStreamImpl
{
	char filename[256];
};

CSWFFileStream::CSWFFileStream(const char* file)
{
	m_pHandle = NULL;
	m_lPos = 0; // redundant on this platform so just initialise it & ignore it..
	m_pImpl = new _fileStreamImpl;
	sprintf(((_fileStreamImpl*)m_pImpl)->filename, "%s", file);
}

CSWFFileStream::~CSWFFileStream()
{
	delete ((_fileStreamImpl*)m_pImpl);
}

bool CSWFFileStream::Open(OpenModeFlags modeflags)
{
	const char* szmode = NULL;

	if( (modeflags & eOpenMode_Read)==eOpenMode_Read )
	{
		if( (modeflags & eOpenMode_Write)==eOpenMode_Write )
			return false;

		if( (modeflags & eOpenMode_Append)==eOpenMode_Append )
			szmode = "rb+";
		else
			szmode = "rb";
	}
	else if( (modeflags & eOpenMode_Write)==eOpenMode_Write )
	{
		if( (modeflags & eOpenMode_Append)==eOpenMode_Append )
			szmode = "wb+";
		else
			szmode = "wb";
	}
	else
	{
		return false;
	}

	m_pHandle = fopen(((_fileStreamImpl*)m_pImpl)->filename, szmode);
	return ( m_pHandle!=NULL );
}

bool CSWFFileStream::Close()
{
	sInt32 ret = fclose((FILE*)m_pHandle);
	return ( ret == 0 );
}

uInt32 CSWFFileStream::Seek(sInt32 offset, eSeekMode mode)
{
	sInt32 orig = SEEK_SET;
	switch(mode)
	{
	case eSeekMode_Curr:	orig = SEEK_CUR; break;
	case eSeekMode_End:		orig = SEEK_END; break;
	}
	uInt32 ret = static_cast<uInt32>( fseek((FILE*)m_pHandle, static_cast<long int>(offset), orig) );
	return ret;
}

uInt32 CSWFFileStream::Tell()
{
	return static_cast<uInt32>( ftell((FILE*)m_pHandle) );
}

uInt32 CSWFFileStream::Read(void* pOutBuffer, uInt32 count, uInt32 size)
{
	uInt32 ret = static_cast<uInt32>( fread(pOutBuffer, static_cast<size_t>(size), static_cast<size_t>(count), (FILE*)m_pHandle) );
	return ret;
}

uInt32 CSWFFileStream::Write(void* pInBuffer, uInt32 count, uInt32 size)
{
	uInt32 ret = static_cast<uInt32>( fwrite(pInBuffer, static_cast<size_t>(size), static_cast<size_t>(count), (FILE*)m_pHandle) );
	return ret;
}

bool CSWFFileStream::Eof()
{
	return ( feof((FILE*)m_pHandle) != 0 );
}